<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
    <div class="box">
        <input id="text" type="text" placeholder="请输入清单">
        <ul id="ul">
            <!-- <li><span>床前明月光</span><button>X</button></li>
            <li><span>疑是地上霜</span><button>X</button></li>
            <li><span>举头望明月</span><button>X</button></li>
            <li><span>低头思故乡</span><button>X</button></li> -->
        </ul>
    </div>
</body>
<script>
    $(()=>{
        var search=[]
        function ergodic(){
            var str='';
            search.forEach((item,index)=>{
                str+=`<li><span>${index+1}.${item}</span><button data-index="${index}">X</button></li>`
            })
            $('#ul').html(str) 
        }
        $($('#text').on('keypress',function(){
            if(event.key=='Enter'){
                search.push($(this).val())/* 向数组添加对象 */
                ergodic() /* 渲染数据 */
                $(this).val('')/* 清空文本框内容 */
            }
        }))
        $($('#ul').on('click','button',function(){
            search.splice($(this).data('index'),1)/* 根据数组下标删除内容 */
            ergodic()/* 重新渲染数据 */
        }))
    })
</script>
</html>